package com.znufe.exam.controller.h5;

import com.znufe.exam.base.BaseApiController;
import com.znufe.exam.base.RestResponse;
import com.znufe.exam.domain.h5.grade.H5StudentExamDetailRequestObject;
import com.znufe.exam.domain.h5.grade.H5StudentExamQuestionAnswerObject;
import com.znufe.exam.service.h5.H5ExamGradeService;
import com.znufe.exam.service.h5.H5ExamPaperService;
import com.znufe.exam.viewmodel.h5.H5ClassExamDotimeRequestVM;
import com.znufe.exam.viewmodel.h5.H5ClassExamDotimeResponseVM;
import com.znufe.exam.viewmodel.h5.H5ExamPaperEditRequestVM;
import com.znufe.exam.viewmodel.h5.grade.*;
import com.znufe.exam.viewmodel.h5.question.H5QuestionEditRequestVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(description = "查询成绩",value="查询成绩" )
@RestController("H5ExamGradeController")
@RequestMapping(value="api/h5/grade")

public class H5ExamGradeController extends BaseApiController {

    @Autowired
    private final H5ExamGradeService h5examGradeService;

    @Autowired
    private final H5ExamPaperService h5examPaperService;

    public H5ExamGradeController(H5ExamGradeService h5examGradeService, H5ExamPaperService h5examPaperService) {
        this.h5examGradeService = h5examGradeService;
        this.h5examPaperService = h5examPaperService;
    }

    @RequestMapping(value = "/get_student_individual_grade", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "通过班级名称、学生名称获得某个学生的个人成绩（按班级名称、学生名称、课程名称查找）",
            notes = "获取某个学生的个人成绩[student_individual_grade_info]")
    public RestResponse<List<H5StudentIndividualGradeResponseVM>> getStudentIndividualGrade(@RequestBody H5StudentIndividualGradeRequestVM model) {
        model = H5StudentIndividualGradeRequestVM.wipeBlank(model);
        List<H5StudentIndividualGradeResponseVM> responseVM = h5examGradeService.getStudentIndividualGradeInfo(model);
        return RestResponse.ok(responseVM);
    }


    @RequestMapping(value = "/get_student_exam_detail", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "通过学生id、试卷id获得某个学生的答卷详情（按学生id、试卷id查找）",
            notes = "获取某个学生生的某张试卷的答卷情况[t_exam_paper_question_customer_answer]")
    public RestResponse<List<H5StudentExamDetailResponseVM>> getStudentExamDetail(@RequestBody H5StudentExamDetailRequestVM model) {
        H5ExamPaperEditRequestVM h5examPaperEditRequestVM = h5examPaperService.examPaperToVM(model.getExamPaperId());
        List<H5QuestionEditRequestVM> questionList = h5examPaperEditRequestVM.getTitleItems()
                .get(0).getQuestionItems();

        List<H5StudentExamDetailResponseVM> vmList = questionList.stream().map(q -> {
            H5StudentExamDetailResponseVM vm = modelMapper.map(q, H5StudentExamDetailResponseVM.class);
            vm.setQuestionId(q.getId());
            //System.out.println(vm.toString() + "     " +  vm.getQuestionId());
            return vm;
        }).collect(Collectors.toList());

        H5StudentExamDetailRequestObject obj = modelMapper.map(model, H5StudentExamDetailRequestObject.class);
        for (H5StudentExamDetailResponseVM vm : vmList) {
            obj.setQuestionId(vm.getQuestionId());
            H5StudentExamQuestionAnswerObject answer = h5examGradeService.selectStuAns(obj);
            if (answer == null) {
                vm.setStudentAnswer("学生未作答");
                vm.setStudentScore(0);
            } else {
                vm.setStudentAnswer(answer.getStuAnswer());
                vm.setStudentScore(answer.getStuScore());
            }
        }
        return RestResponse.ok(vmList);
    }



    @RequestMapping(value = "/get_class_exam_grade_rank", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "通过班级id、试卷id获得某个班级某个试卷的学生成绩排名（按班级id、试卷id查找）",
            notes = "获取某个班的某场考试的成绩排名[student_exam_grade_info]")
    public RestResponse<List<H5ClassExamGradeResponseVM>> getClassExamGradeRank(@RequestBody H5ClassExamGradeRequestVM model) {
        if (null == model.getClassId() || null == model.getExamPaperId()) {
            return new RestResponse<>(2,"班级id与试卷id均不能为空！");
        }
        List<H5ClassExamGradeResponseVM> responseVM = h5examGradeService.selectClassExamGrade(model);
        return RestResponse.ok(responseVM);
    }

    @RequestMapping(value = "/get_class_exam_dotime_rank", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "通过班级id、试卷id获得某个班级某个试卷的学生答题次数排名（按班级id、试卷id查找）",
            notes = "获取某个班的某场考试的学生答题次数排名[student_exam_doTime_info]")
    public RestResponse<List<H5ClassExamDotimeResponseVM>> getClassExamGradeRank(@RequestBody H5ClassExamDotimeRequestVM model) {
        if (null == model.getClassId() || null == model.getExamPaperId()) {
            return new RestResponse<>(2,"班级id与试卷id均不能为空！");
        }
        List<H5ClassExamDotimeResponseVM> responseVM = h5examGradeService.selectClassExamDoTime(model);
        return RestResponse.ok(responseVM);
    }


}
